package com.example.antivity;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.Model.movie.Movie;
import com.example.Model.movie.Moviedesc;
import com.example.Model.music.MusicComment;
import com.example.UI.MyScrollview;
import com.example.UI.Mylistview;
import com.example.adapter.MovieAdapter;
import com.example.retrofitinterface.movie.MovieComInter;
import com.example.retrofitinterface.movie.MovieDescInter;
import com.example.retrofitinterface.movie.MovieInter;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebViewClient;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import sdstcom.lbq.one.R;
import sdstcom.lbq.one.lbq.Databeans.DbBean;
import sdstcom.lbq.one.lbq.LBQapplication;

public class MovieActivity extends AppCompatActivity {
    private String url2;
    private String movieurl1="http://v3.wufazhuce.com:8000/api/movie/";
    private String movieurl3="/story/1/0?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91";
    private String movieurl;
    //private String movieurl = "http://v3.wufazhuce.com:8000/api/movie/258/story/1/0?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91";
    private String moviedescurl1="http://v3.wufazhuce.com:8000/api/movie/detail/";


    private String moviedescurl3="?platform=android&version=4.0.2&source=summary&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&source_id=9241&channel=91";
    private String moviedescurl;
    //private String moviedescurl = "http://v3.wufazhuce.com:8000/api/movie/detail/258?platform=android&version=4.0.2&source=summary&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&source_id=9241&channel=91";
   private String commenturl1="http://v3.wufazhuce.com:8000/api/comment/praiseandtime/movie/";
   private String commenturl3="/0?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91";
    private String commenturl;
    //private String commenturl = "http://v3.wufazhuce.com:8000/api/comment/praiseandtime/movie/258/0?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91";
    private Toolbar tb;
    private ImageView coveriv;
    private TextView title;
    private TextView moviename;
    private Button descbtn;
    //需要外界传递
    private String id = "258";
    private String summary;
    private PopupWindow moviewindow;
    private View popview;
    //电影详情相关控件
    private ImageView movieposter;
    private TextView moviedescname;
    private TextView moviedescsummary;
    private TextView movieactor;
    private TextView moviedescintro;
    private TextView moviestoryauthor;
    private com.tencent.smtt.sdk.WebView wv;
    private TextView editor;
    private TextView editoremail;
    private ImageView authoriv;
    private TextView authorname;
    private TextView authordesc;
    private MovieAdapter adapter;
    private ArrayList<MusicComment.DataBeanX.DataBean> list;
    private Mylistview lv;




    private String DBtitle;
    private DbManager dbManager;
    private boolean ifpress = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_movie);
        tb = (Toolbar) findViewById(R.id.movie_tb);
        tb.setTitle("");
        setSupportActionBar(tb);
        tb.setNavigationIcon(R.mipmap.return_image_4_0);
        //返回监听
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        init();
    }

    private void init() {
        url2 = getIntent().getStringExtra("itemid");
        movieurl= movieurl1+url2+movieurl3;
        moviedescurl = moviedescurl1+url2+moviedescurl3;
        commenturl = commenturl1+url2+commenturl3;
        coveriv  = (ImageView) findViewById(R.id.movie_cover_iv);
        title = (TextView) findViewById(R.id.movie_storytitle_tv);
        moviename = (TextView) findViewById(R.id.movie_name_tv);
        moviestoryauthor = (TextView) findViewById(R.id.movie_storyauthor_tv);
        wv = (com.tencent.smtt.sdk.WebView) findViewById(R.id.movie_wb);
        initwv();
        editor = (TextView) findViewById(R.id.movie_edit_tv);
        editoremail = (TextView) findViewById(R.id.movie_edi_emial_tv);
        authoriv = (ImageView) findViewById(R.id.movie_author_iv);
        authorname = (TextView) findViewById(R.id.movie_author_tv);
        authordesc = (TextView) findViewById(R.id.movie_author_desc);
        lv = (Mylistview) findViewById(R.id.movie_lv);
        list = new ArrayList<>();
        adapter = new MovieAdapter(list,this);
        lv.setAdapter(adapter);

        //获取屏幕的宽高
        int w=getResources().getDisplayMetrics().widthPixels;
        int h=getResources().getDisplayMetrics().heightPixels;
        //初始化popview对象
        popview = View.inflate(this,R.layout.moviepop,null);
//        popview.setAnimation(AnimationUtils.loadAnimation(this,R.anim.pw_show));
        //初始化window中相关控件
        movieposter = (ImageView) popview.findViewById(R.id.movie_poster_iv);
        moviedescname = (TextView) popview.findViewById(R.id.moviedesec_moviename_tv);
        moviedescsummary = (TextView) popview.findViewById(R.id.moviedesc_summary_tv);
        movieactor = (TextView) popview.findViewById(R.id.moviedesc_actor_tv);
        moviedescintro = (TextView) popview.findViewById(R.id.moviedesc_officialstory_tv);
        //创建popupwindow对象
        moviewindow = new PopupWindow(popview,w,h);
        //创建colordrawable对象
        ColorDrawable dw = new ColorDrawable(0xffffffff);
        moviewindow.setBackgroundDrawable(dw);
        //设定动画效果
        moviewindow.setAnimationStyle(R.style.popwin_anim_style);
        moviewindow.update();
        //找到pop中的scrollview
         MyScrollview sv = (MyScrollview) popview.findViewById(R.id.movie_desc_sv);
        sv.setWindow(moviewindow);
        //popview绑定触摸监听

        loadmovie();
        loadmoviedesc();
        loadcomment();

        dbManager= x.getDb(((LBQapplication)getApplication()).config);
    }
    //下载评论
    public void loadcomment() {
        new Retrofit.Builder().baseUrl("http://v3.wufazhuce.com:8000/").
                addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build().create(MovieComInter.class).getcomment(commenturl).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
               subscribe(new Action1<MusicComment>() {
                   @Override
                   public void call(MusicComment musicComment) {
                       list.addAll(musicComment.getData().getData());
                       adapter.notifyDataSetChanged();
                   }
               });

    }
    private void initwv() {
//        Log.e("PATH",getIntent().getStringExtra("path"));

        //wv.loadUrl(getIntent().getStringExtra("path"));
        WebSettings settings = wv.getSettings();
        settings.setJavaScriptEnabled(true);

        // 设置加载进来的页面自适应手机屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(true);

        settings.setTextSize(WebSettings.TextSize.LARGEST);

        wv.setWebViewClient(new WebViewClient(){

            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);
                return true;
            }
        });
    }

    //下载电影文章
    public void loadmovie(){
        new Retrofit.Builder().baseUrl("http://v3.wufazhuce.com:8000/").
                addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build().create(MovieInter.class).getmovie(movieurl).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Action1<Movie>() {
                    @Override
                    public void call(Movie movie) {
                        //设置文章标题
                        title.setText(movie.getData().getData().get(0).getTitle());
                        DBtitle=movie.getData().getData().get(0).getTitle();
                        //summary赋值
                        summary = movie.getData().getData().get(0).getSummary();
                        Log.e("xxx", "call: "+summary );
                        //电影详情页中summary
                        moviedescsummary.setText(summary);
                        moviestoryauthor.setText(movie.getData().getData().get(0).getUser().getUser_name());
                        wv.loadData(movie.getData().getData().get(0).getContent(),"text/html; charset=UTF-8",null);
                        editor.setText(movie.getData().getData().get(0).getCharge_edt());
                        editoremail.setText(movie.getData().getData().get(0).getEditor_email());
                        Glide.with(getApplication()).load(movie.getData().getData().get(0).getUser().getWeb_url()).into(authoriv);
                        authorname.setText(movie.getData().getData().get(0).getUser().getUser_name());
                        authordesc.setText(movie.getData().getData().get(0).getUser().getDesc());
                    }
                });
    }
    //下载电影详情
    public void loadmoviedesc(){
        new Retrofit.Builder().baseUrl("http://v3.wufazhuce.com:8000/").
                addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build().create(MovieDescInter.class).getmoviedesc(moviedescurl).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Action1<Moviedesc>() {
                    @Override
                    public void call(Moviedesc moviedesc) {
                        //下载电影海报
                        Glide.with(getApplication()).load(moviedesc.getData().getDetailcover()).into(coveriv);
                        //设置电影名字
                        moviename.setText("《"+moviedesc.getData().getTitle()+"》");
                        //设置电影详情页信息
                        Glide.with(getApplication()).load(moviedesc.getData().getPoster()).into(movieposter);
                        moviedescname.setText(moviedesc.getData().getTitle());
                        movieactor.setText(moviedesc.getData().getInfo());
                        Log.e("xxx", "call: "+ moviedesc.getData().getOfficialstory());
                        moviedescintro.setText(moviedesc.getData().getOfficialstory());

                    }
                });

    }
    //显示电影详情
    public void desc(View view){

        moviewindow.showAtLocation(findViewById(R.id.activity_movie), Gravity.TOP,0,0);

    }
    public void collection(View view) throws DbException {
        if (ifpress == false) {
            view.setPressed(true);
            DbBean dbBean = new DbBean();
            dbBean.setItemid(url2);
            dbBean.setType("movie");
            dbBean.setTitle(DBtitle);
            dbBean.setSubtype("movie");
            DbBean bean=dbManager.findById(DbBean.class,url2);
//            Log.e("TS", "collection: "+bean);
//            String sql="SELECT * FROM myonetable WHERE itemid="+url2;
            if (bean==null)
            {
                dbManager.save(dbBean);
                Toast.makeText(this,"收藏完成",Toast.LENGTH_SHORT).show();}
            ifpress=true;
        }else if (ifpress==true)
        {
            view.setPressed(false);
            dbManager.deleteById(DbBean.class,url2);
            ifpress=false;
            Toast.makeText(this,"取消收藏",Toast.LENGTH_SHORT).show();
        }
    }
}
